package com.txx.app.main.modules.web.js;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.txx.app.main.commom.ui.MyLog;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.function.CheckSign;
import com.txx.app.main.modules.web.function.Decrypt;
import com.txx.app.main.modules.web.function.Encrypt;
import com.txx.app.main.modules.web.function.GetSign;
import com.txx.app.main.modules.web.function.H5GetPlanbookParams;
import com.txx.app.main.modules.web.function.H5GetUserInfoExternal;
import com.txx.app.main.modules.web.function.H5GetUsersInfoByNative;
import com.txx.app.main.modules.web.function.JumpToCustom;
import com.txx.app.main.modules.web.function.JumpToMyCard;
import com.txx.app.main.modules.web.function.JumpToNativeController;
import com.txx.app.main.modules.web.function.JumpToNativePage;
import com.txx.app.main.modules.web.function.StatusStyle;
import com.txx.app.main.modules.web.function.Switch900Account;
import com.txx.app.main.modules.web.function.live.EnterLivePage;
import com.txx.app.main.utils.MsgUtil;

import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * J和H5交互的具体类
 * Created by REN SHI QIAN on 2017/12/11.
 * <p>
 * JS和H5交互有以下几种：
 * 1. android  计算完，直接传结果  考虑加密
 * 2。 js 直接调android 方法，不传结果  考虑返回键处理
 * 3. js 调android 方法，android 调用其他页面 ，然后返回结果 考虑扫一扫  考虑所有方法放在一个bean 中，方便存取，每个方法 用一个bean 来获取，通过 wp 来获取
 */
public class TXXJSInterface implements IJsInterface {

    private final String LOG_TAG = this.getClass ().getSimpleName ();
    //
    private WebCommonFragment mWebCommonFragment;
    private Handler mHandler;
    private ConcurrentHashMap<String, H5BasePlugin> mPluginMap = new ConcurrentHashMap<> ();

//  数据处理


    public TXXJSInterface(WebCommonFragment mWebCommonFragment) {
        this.mWebCommonFragment = mWebCommonFragment;
        initHandler ();
    }


    private void initHandler() {
        mHandler = new Handler (Looper.getMainLooper ());
    }

    @Override
    public void onRelease() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages (null);
        }
        cleanAllData ();
        mWebCommonFragment = null;
    }

    //释放js 相关资源
    private void cleanAllData() {
        Iterator it = mPluginMap.entrySet ().iterator ();
        while (it.hasNext ()) {
            Map.Entry entry = (Map.Entry) it.next ();
            H5BasePlugin h5BasePlugin = (H5BasePlugin) entry.getValue ();
            h5BasePlugin.onRelease ();
        }
        mPluginMap.clear ();
    }


    /**
     * js调用本地获取用户信息的方法 调用android.getUserInfoByNative方法
     * 因为 JavaBridge 需要在主线程进行回调  直接回调在子线程，需要做  线程切换处理
     * h5 调用方法后直接返回结果
     */
    @JavascriptInterface
    public void getUserInfoByNative(final String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "getUserInfoByNative";
        H5GetUsersInfoByNative h5GetUsersInfoByNative;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof H5GetUsersInfoByNative) {
                h5GetUsersInfoByNative = (H5GetUsersInfoByNative) basePlugin;
            } else {
                h5GetUsersInfoByNative = new H5GetUsersInfoByNative ();
                mPluginMap.put (mothedName, h5GetUsersInfoByNative);
            }
        } else {
            h5GetUsersInfoByNative = new H5GetUsersInfoByNative ();
            mPluginMap.put (mothedName, h5GetUsersInfoByNative);
        }
        h5GetUsersInfoByNative.handleEvent (event, mWebCommonFragment);


//       在主线程 需发送信息
//        mHandler.post (new Runnable () {
//            @Override
//            public void run() {
//                String mothedName= ThreadJudgeUtils.getMethodName ();
//                H5Event event=new H5Event (mothedName,info);
//                H5GetUsersInfoByNative h5GetUsersInfoByNative=new H5GetUsersInfoByNative();
//                h5GetUsersInfoByNative.handleAsynEvent (event,mWebCommonFragment);
//            }
//        });

    }


    /**
     * 获取加签信息的方法
     *
     * @param info
     */
    @JavascriptInterface
    public void getSign(String info) {

        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "getSign";
        GetSign getSign;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof GetSign) {
                getSign = (GetSign) basePlugin;
            } else {
                getSign = new GetSign ();
                mPluginMap.put (mothedName, getSign);
            }
        } else {
            getSign = new GetSign ();
            mPluginMap.put (mothedName, getSign);
        }
        getSign.handleEvent (event, mWebCommonFragment);


    }

    /**
     * 获取解签信息的方法
     *
     * @param info
     */
    @JavascriptInterface
    public void checkSign(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "checkSign";
        CheckSign checkSign;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof CheckSign) {
                checkSign = (CheckSign) basePlugin;
            } else {
                checkSign = new CheckSign ();
                mPluginMap.put (mothedName, checkSign);
            }
        } else {
            checkSign = new CheckSign ();
            mPluginMap.put (mothedName, checkSign);
        }
        checkSign.handleEvent (event, mWebCommonFragment);
    }


    /**
     * 获取加密信息的方法
     *
     * @param info
     */
    @JavascriptInterface
    public void encrypt(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "encrypt";
        Encrypt ownPlugin;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof Encrypt) {
                ownPlugin = (Encrypt) basePlugin;
            } else {
                ownPlugin = new Encrypt ();
                mPluginMap.put (mothedName, ownPlugin);
            }
        } else {
            ownPlugin = new Encrypt ();
            mPluginMap.put (mothedName, ownPlugin);
        }
        ownPlugin.handleEvent (event, mWebCommonFragment);


    }

    /**
     * 获取解密信息的方法
     *
     * @param info
     */
    @JavascriptInterface
    public void decrypt(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "decrypt";
        Decrypt ownPlugin;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof Decrypt) {
                ownPlugin = (Decrypt) basePlugin;
            } else {
                ownPlugin = new Decrypt ();
                mPluginMap.put (mothedName, ownPlugin);
            }
        } else {
            ownPlugin = new Decrypt ();
            mPluginMap.put (mothedName, ownPlugin);
        }
        ownPlugin.handleEvent (event, mWebCommonFragment);

    }


    /**
     * 由于状态栏的字体颜色需要设置，为此拓展了一个原生的交互方法
     * 方法名：setStatusStyle
     * 入参：style 值：0 黑色，1 白色
     */
    @JavascriptInterface
    public void setStatusStyle(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "setStatusStyle";
        StatusStyle ownPlugin;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof StatusStyle) {
                ownPlugin = (StatusStyle) basePlugin;
            } else {
                ownPlugin = new StatusStyle ();
                mPluginMap.put (mothedName, ownPlugin);
            }
        } else {
            ownPlugin = new StatusStyle ();
            mPluginMap.put (mothedName, ownPlugin);
        }
        ownPlugin.handleEvent (event, mWebCommonFragment);


    }

    /**
     * 打开原生功能
     * zcc
     * {
     * "pluginName": "app.jiazu",
     * "className": "com.txx.app.jiazu.jiazu_module.main.activity.FamilyMainActivity",
     * "params": {
     * "AGTNO_TAG": "",
     * "mode_token": "private"
     * },
     * "callBackName": ""
     * }
     */
    @JavascriptInterface
    public void jumpToNativeController(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "jumpToNativeController";
        JumpToNativeController ownPlugin;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof JumpToNativeController) {
                ownPlugin = (JumpToNativeController) basePlugin;
            } else {
                ownPlugin = new JumpToNativeController ();
                mPluginMap.put (mothedName, ownPlugin);
            }
        } else {
            ownPlugin = new JumpToNativeController ();
            mPluginMap.put (mothedName, ownPlugin);
        }
        ownPlugin.handleEvent (event, mWebCommonFragment);
    }


    /**
     * H5 跳转原生页面
     */
    @JavascriptInterface
    public void jumpToNativePage(String result) {
        if (TextUtils.isEmpty (result)) {
            return;
        }
//        默认是异步
        String mothedName = "jumpToNativePage";
        JumpToNativePage ownPlugin;
        H5Event event = new H5Event (mothedName, result);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof JumpToNativePage) {
                ownPlugin = (JumpToNativePage) basePlugin;
            } else {
                ownPlugin = new JumpToNativePage ();
                mPluginMap.put (mothedName, ownPlugin);
            }
        } else {
            ownPlugin = new JumpToNativePage ();
            mPluginMap.put (mothedName, ownPlugin);
        }
        ownPlugin.handleEvent (event, mWebCommonFragment);

    }

    //    案例 startactivityforresult
    @JavascriptInterface
    public void H5ScanByNative(final String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
//        String mothedName= ThreadJudgeUtils.getMethodName ();
//        H5Event event=new H5Event (mothedName,info);
//        H5ScanByNative h5ScanByNative=new H5ScanByNative ();
//        h5ScanByNative.handleAsynEvent (event,mWebCommonFragment);
//       在主线程 发送信息
//        mHandler.post (new Runnable () {
//            @Override
//            public void run() {
//                String mothedName= ThreadJudgeUtils.getMethodName ();
//                H5Event event=new H5Event (mothedName,info);
//                H5GetUsersInfoByNative h5GetUsersInfoByNative=new H5GetUsersInfoByNative();
//                h5GetUsersInfoByNative.handleAsynEvent (event,mWebCommonFragment);
//            }
//        });

    }


    @Override
    public String getName() {
        return "android";
    }

    @Override
    public void onResume() {
        Iterator it = mPluginMap.entrySet ().iterator ();
        while (it.hasNext ()) {
            Map.Entry entry = (Map.Entry) it.next ();
            H5BasePlugin h5BasePlugin = (H5BasePlugin) entry.getValue ();
            h5BasePlugin.onReusme ();
        }
    }

    @JavascriptInterface
    public void getUserInfoExternal(final String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "getUserInfoExternal";
        H5GetUserInfoExternal h5GetUserInfoExternal;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof H5GetUserInfoExternal) {
                h5GetUserInfoExternal = (H5GetUserInfoExternal) basePlugin;
            } else {
                h5GetUserInfoExternal = new H5GetUserInfoExternal ();
                mPluginMap.put (mothedName, h5GetUserInfoExternal);
            }
        } else {
            h5GetUserInfoExternal = new H5GetUserInfoExternal ();
            mPluginMap.put (mothedName, h5GetUserInfoExternal);
        }
        h5GetUserInfoExternal.handleEvent (event, mWebCommonFragment);


//       在主线程 需发送信息
//        mHandler.post (new Runnable () {
//            @Override
//            public void run() {
//                String mothedName= ThreadJudgeUtils.getMethodName ();
//                H5Event event=new H5Event (mothedName,info);
//                H5GetUsersInfoByNative h5GetUsersInfoByNative=new H5GetUsersInfoByNative();
//                h5GetUsersInfoByNative.handleAsynEvent (event,mWebCommonFragment);
//            }
//        });

    }

    /**
     * 调用方法通知app重新获取最新全部未读消息数量
     */
    @JavascriptInterface
    public void postNotification(String info) {
        MyLog.wtf(LOG_TAG, info);
        MsgUtil.getInstance().receiveNewMsg();
    }

    /**
     * 获取计划书参数
     *
     * @param info
     */
    @JavascriptInterface
    public void getPlanbookParams(String info) {
        if (TextUtils.isEmpty (info)) {
            return;
        }
//        默认是异步
        String mothedName = "getPlanbookParams";
        H5GetPlanbookParams h5GetPlanbookParams;
        H5Event event = new H5Event (mothedName, info);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof H5GetUserInfoExternal) {
                h5GetPlanbookParams = (H5GetPlanbookParams) basePlugin;
            } else {
                h5GetPlanbookParams = new H5GetPlanbookParams ();
                mPluginMap.put (mothedName, h5GetPlanbookParams);
            }
        } else {
            h5GetPlanbookParams = new H5GetPlanbookParams ();
            mPluginMap.put (mothedName, h5GetPlanbookParams);
        }
        h5GetPlanbookParams.handleEvent (event, mWebCommonFragment);

    }

    @JavascriptInterface
    public void jumpToCustom() {
        String mothedName = "jumpToCustom";
        JumpToCustom jumpToCustomF;
        H5Event event = new H5Event (mothedName, null);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof JumpToCustom) {
                jumpToCustomF = (JumpToCustom) basePlugin;
            } else {
                jumpToCustomF = new JumpToCustom ();
                mPluginMap.put (mothedName, jumpToCustomF);
            }
        } else {
            jumpToCustomF = new JumpToCustom ();
            mPluginMap.put (mothedName, jumpToCustomF);
        }
        jumpToCustomF.handleEvent (event, mWebCommonFragment);
    }

    @JavascriptInterface
    public void jumpToMyCard() {
        String mothedName = "jumpToMyCard";
        JumpToMyCard jumpToMyCardF;
        H5Event event = new H5Event (mothedName, null);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof JumpToMyCard) {
                jumpToMyCardF = (JumpToMyCard) basePlugin;
            } else {
                jumpToMyCardF = new JumpToMyCard ();
                mPluginMap.put (mothedName, jumpToMyCardF);
            }
        } else {
            jumpToMyCardF = new JumpToMyCard ();
            mPluginMap.put (mothedName, jumpToMyCardF);
        }
        jumpToMyCardF.handleEvent (event, mWebCommonFragment);

    }

    @JavascriptInterface
    public void switch900Account(String json) {
        if (TextUtils.isEmpty (json)) {
            return;
        }
        String mothedName = "switch900Account";
        Switch900Account switch900AccountF;
        H5Event event = new H5Event (mothedName, json);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof Switch900Account) {
                switch900AccountF = (Switch900Account) basePlugin;
            } else {
                switch900AccountF = new Switch900Account ();
                mPluginMap.put (mothedName, switch900AccountF);
            }
        } else {
            switch900AccountF = new Switch900Account ();
            mPluginMap.put (mothedName, switch900AccountF);
        }
        switch900AccountF.handleEvent (event, mWebCommonFragment);

    }



    @android.webkit.JavascriptInterface
    public void enterLivePage(String json) {
        if (android.text.TextUtils.isEmpty (json)) {
            return;
        }
        String mothedName = "enterLivePage";
        EnterLivePage enterLivePage;
        H5Event event = new H5Event (mothedName, json);
        if (mPluginMap.get (mothedName) != null) {
            H5BasePlugin basePlugin = mPluginMap.get (mothedName);
            if (basePlugin instanceof EnterLivePage) {
                enterLivePage = (EnterLivePage) basePlugin;
            } else {
                enterLivePage = new EnterLivePage ();
                mPluginMap.put (mothedName, enterLivePage);
            }
        } else {
            enterLivePage = new EnterLivePage ();
            mPluginMap.put (mothedName, enterLivePage);
        }
        enterLivePage.handleEvent (event, mWebCommonFragment);

    }
}